<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/12/17
 * Time: 2:35 PM
 */

namespace App\BO\OrderStatus\State\Refund;

use App\BO\OrderStatus\Events\OrderRefundSuccessEvent;
use App\BO\OrderStatus\OrderStateContext;
use App\BO\OrderStatus\State\AOrderState;

class RefundWaitingThirdPartNotifyState extends AOrderState
{
    protected $state = '等待第三方平台通知';

    /**
     * 前端显示用
     * @var string
     */
    protected $desc = '退款中';

    /**
     * @return bool
     */
    public function hasRefundInfo()
    {
        return true;
    }

    /**
     * 将状态置为退款中
     * @param bool $success
     * @throws \Exception
     */
    public function refund(bool $success = true)
    {
        if ($success) {
            $this->refundSuccess();
        } else {
            $this->refundFail();
        }
    }

    private function refundSuccess()
    {
        $this->orderStateModifyStore->toRefundSuccess();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(RefundSuccessState::class));
        event(new OrderRefundSuccessEvent($this->stateContext->getOrder()));
    }

    private function refundFail()
    {
        $this->orderStateModifyStore->toRefundFailed();
        $this->stateContext->setOrderState(OrderStateContext::getInstance(RefundFailedState::class));
    }
}
